package link.tengfei.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ling.tengfei.service.AdminListService;
import ling.tengfei.service.AdminListServiceImpl;
import link.tengfei.bean.AdminUser;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class AdminListServlet
 */
@WebServlet("/adminlist")
public class AdminListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		AdminListService adminListService = new AdminListServiceImpl();
		PrintWriter writer = response.getWriter();
		String action = request.getParameter("action");
		if (action == null || action == "") {
			request.getRequestDispatcher("/WEB-INF/admin/admin_list.html").forward(request, response);
		} else if (action != null && action.equals("totalPage")) {
			int totalNum = adminListService.findNum();
			int totalPage = (totalNum % 10 == 0) ? totalNum / 10 : totalNum / 10 + 1;
			writer.print(totalPage);
		} else if (action != null && action.equals("changePage")) {
			int page = Integer.parseInt(request.getParameter("page")) * 10 - 10;
			List<AdminUser> users = adminListService.findByPage(page);
			JSONArray json = JSONArray.fromObject(users);
			writer.print(json);
		} else if (action != null && action.equals("searchPage")) {
			int page = Integer.parseInt(request.getParameter("page")) * 10 - 10;
			String name = request.getParameter("name");
			List<AdminUser> users = adminListService.findByName(page, name);
			JSONArray json = JSONArray.fromObject(users);
			writer.print(json);
		} else if (action != null && action.equals("totalSearch")) {
			String name = request.getParameter("name");
			int total = adminListService.findNumByName(name);
			int totalPage = (total % 10 == 0) ? total / 10 : total / 10 + 1;
			writer.print(totalPage);
		} else if (action != null & action.equals("resetPwd")) {
			int id = Integer.parseInt(request.getParameter("id"));
			int result = adminListService.resetPwdById(id);
			if (result == 1) {
				writer.print(1);
			} else {
				writer.print(2);
			}
		} else if (action != null && action.equals("delete")) {
			int id = Integer.parseInt(request.getParameter("id"));
			int state = Integer.parseInt(request.getParameter("state"));
			if (state==1) {
				state=0;
			}else {
				state=1;
			}
			int result = adminListService.delete(id,state);
			if (result == 1) {
				writer.print(1);
			} else {
				writer.print(2);
			}
		} else if (action != null && action.equals("getState")) {
			int id = Integer.parseInt(request.getParameter("id"));
			int state = adminListService.getStateById(id);
			writer.print(state);
		} else if (action != null && action.equals("detail")) {
			int id = Integer.parseInt(request.getParameter("id"));
			AdminUser user = adminListService.findUserById(id);
			JSONObject json = JSONObject.fromObject(user);
			writer.print(json);
		}
	}

}
